import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

const catchUrls = ['m.ctrip.com/', 'm.ctrip.com/html5/', 'm.ctrip.com/html5'];

class WebView extends StatefulWidget {
  final String url;
  final String? statusBarColor;
  final String title;
  final bool? hideAppBar;
  final bool backForbid;

  const WebView(
      {super.key,
      required this.url,
      this.statusBarColor = "ffffff",
      this.title = "",
      this.hideAppBar = false,
      this.backForbid = false});

  @override
  State<WebView> createState() => _WebViewState();
}

class _WebViewState extends State<WebView> {
  final WebViewController _controller = WebViewController();

  @override
  void initState() {
    super.initState();
    _controller
      ..enableZoom(true)
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(Colors.white)
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            // Update loading bar.
            print('onProgress progress:$progress');
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {
            print('onPageFinished url:$url');
          },
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            print('onNavigationRequest url:${request.url}');
            for (final value in catchUrls) {
              if (request.url.endsWith(value)) {
                Navigator.pop(context);
                print('url:${request.url}, NavigationDecision.prevent');
                return NavigationDecision.prevent;
              }
            }
            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(widget.url));
  }

  @override
  Widget build(BuildContext context) {
    Color backButtonColor;
    String statusBarColorStr = widget.statusBarColor ?? 'ffffff';
    if (statusBarColorStr == 'ffffff') {
      backButtonColor = Colors.black;
    } else {
      backButtonColor = Colors.white;
    }
    Color statusBarColor = Color(int.parse('0xFF$statusBarColorStr'));
    return Scaffold(
      //需要适配有刘海的屏幕适配
      appBar: widget.hideAppBar ?? false
          ? PreferredSize(
              preferredSize: const Size.fromHeight(0),
              child: AppBar(
                automaticallyImplyLeading: false,
                backgroundColor: statusBarColor,
              ))
          : AppBar(
              leading: GestureDetector(
                onTap: () {
                  Navigator.pop(context);
                },
                child: Icon(Icons.close, color: backButtonColor, size: 26),
              ),
              title: Text(
                widget.title,
                style: TextStyle(color: backButtonColor, fontSize: 20),
              ),
              backgroundColor: statusBarColor,
            ),
      body: WebViewWidget(controller: _controller),
    );
  }
}
